package com.caishi.lkx.exam.service.impl;

import com.caishi.lkx.exam.mapper.PackageSkuAttrMapper;
import com.caishi.lkx.exam.model.PackageSkuAttrModel;
import com.caishi.lkx.exam.service.IPackageSkuAttrService;
import com.zzw.common.entity.IBaseMapper;
import com.zzw.common.exception.BizRuntimeException;
import com.caishi.lkx.exam.ExamResultCode;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PackageSkuAttrServiceImpl implements IPackageSkuAttrService {

    @Resource
    private PackageSkuAttrMapper packageSkuAttrMapper;


    @Override
    public IBaseMapper<PackageSkuAttrModel, String> getMapper() {
        return packageSkuAttrMapper;
    }

    @Override
    public PackageSkuAttrModel insert(PackageSkuAttrModel entity) {
        try {
            return getMapper().insert(entity) == 1 ? entity : null;
        } catch (Exception e) {
            if (e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                throw new BizRuntimeException(ExamResultCode.codeExist);
            }
            log.error(e.getMessage());
            throw e;
        }

    }

    @Override
    public List<String> getNameListByIds(List<String> skuIds) {
        return this.selectBatchIds(skuIds).stream().map(PackageSkuAttrModel::getName).collect(Collectors.toList());
    }

    @Override
    public PackageSkuAttrModel builder() {
        return new PackageSkuAttrModel();
    }
}
